#	CheckWindowIn - check a window in
#
#	Usage:	CheckWindowIn window
#
#	Status:	CheckWindowIn may return the following status values:
#
#			0		the window was checked in
#			1		error
#			4		the user canceled
#
#	CheckWindowIn presents a dialog that allows the user to type
#	in a short comment that indicates what changes have been made
#	to the file since it was checked out. These will be logged into
#	the project, and, if the file has a LastChange marker, will be
#	logged into the file.
#
#	© 1988, 1989 Apple Computer, Inc.
#	All rights reserved.

Set Exit 0

# the following helps to debug until MPW sends echos to Dev:Console instead of Dev:StdErr
If {Echo} == 1
	Set somewhere "·· '{Worksheet}¶'"
Else
	Set somewhere '· Dev:Null'
End

Begin

	Set OldComment "{SystemFolder}OldComment"
	Set NewComment "{SystemFolder}NewComment"
	Set CheckInErrors "{SystemFolder}CheckInErrors"
	Set Window "{1}"

	# get short name of window

	If "{Window}" !~ /:*([Â:]+:*)*([Â:]+)¨1/
		Exit 1							# bad window parameter
	End
	Set Short "{¨1}"					# get short name for dialogs
	Set Cant "Ò{Short}Ó canÕt be checked in"

	# check what project the window belongs to

	# *** what about a files that are new to a project?
	Set Info "`ProjectInfo "{Window}"`"
	Unset New
	Unset Version
###	If "{Info}" !~ /ÅProject: (Åº)¨1 Å/
	If "{Info}" !~ /ÅProject: (Å)¨1     Checked out: Å/
		If `Project` == ""
			Alert "{Cant} because there are no projects mounted."
			Exit 1
		End

		Set New "-new -project `GetListItem -r 10 ¶`MountProject -pp -s -r¶` -d ¶`Project¶` ¶
			-m "Ò{Short}Ó isnÕt in a project. Choose one to add it to:" ; Set GetListItemStatus {Status}`"
		If {GetListItemStatus} != 0
			Exit 1
		End

		If "{InitialVersion}" != ""
			Set Version ",{InitialVersion}"
		End
	Else
		Set Project "{¨1}"

		# check if the project is mounted

		ProjectInfo -project "{Project}" -only
		If {Status} != 0
			Alert "{Cant} because the project Ò{Project}Ó is not mounted."
			Exit 1
		End

		# ensure that the window is checked out

		If "{Info}" !~ /[Â,]+,[Â ¶t]+[¶+¶*]Å/
			Alert "{Cant} because it wasnÕt checked out."
			Exit 1
		End
	End

	Echo -n >"{OldComment}"					# start with an empty file
	If {ProjectHeaders}
		Mark -y ¤ OldSelection "{Window}"	# mark the selection
		GetLastChange "{Window}" "{OldComment}"
	End
	If ({Status} ­ 0) || Â {ProjectHeaders}
		Set Info "`ProjectInfo "{Window}" -comments`"
		If "{Info}" =~ /ÅComment: +(Å)¨1/
			Echo -n "{¨1}" >"{OldComment}"
		End
	End

	Set IgnoreCmdPeriod 1
	CoolRequest -q -d "What did you change in Ò{Short}Ó?" <"{OldComment}" >"{NewComment}"
	Set CommentStatus {Status}
	Set IgnoreCmdPeriod 0
	Equal -d -q "{OldComment}" "{NewComment}"
	Set EqualStatus {Status}
	Delete -i "{OldComment}"
	If {CommentStatus} != 0
		If {ProjectHeaders}
			Find OldSelection "{Window}"
			Unmark OldSelection "{Window}"
		End
		Delete -i "{NewComment}"
		If {CommentStatus} == 4
			Exit 4
		End
		Exit 1
	End

	If {ProjectHeaders}
		If {EqualStatus}
			# add/modify a change in the change history

			Evaluate "`Files -n -q -x m "{Window}"`" =~ /[Â¶/]+ ([1-9]Å)¨1/
			Set Date "{¨1}"					# get date before adding change

			Clear LastChange "{Window}"		# get rid of old change
			AddChange "{Window}" "{NewComment}"
			Set AddChangeStatus {Status}	# remember if we canceled
			Find ¥ "{Window}"				# go to top
			Replace -c ° /[ ¶t]+°/ '' "{Window}"	# trim all trailing whitespace

			SetFile -m "{Date}" "{Window}"	# set date back to before change
		End

		Find OldSelection "{Window}"
		Unmark OldSelection "{Window}"

		Save "{Window}"						# save before checking in

		If {AddChangeStatus} == 4
			Exit 4
		End
	End

	# *** what about a way to keep working (keep the modifiable)?
	# *** what about introducing a new branch?
	# *** what about entering a "task"?
	CheckIn {New} -cf "{NewComment}" "{Window}{Version}" ·"{CheckInErrors}"
	Set CheckInStatus {Status}
	Delete -i "{NewComment}"
	If {CheckInStatus} != 0
		Alert "{Cant}. You may need to check it in on a branch.¶n`Catenate "{CheckInErrors}"`"
		Exit 1
	End
	Delete -i "{CheckInErrors}"

End {somewhere}
